package com.sky.service.admin.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.admin.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangqi
 * @date 2024/1/8 14:29
 */
@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 获取工作台的今日数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */
        //1.获取营业额
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);

        map.put("status", Orders.COMPLETED);
        Double turnover = ordersMapper.turnoverByMap(map);
        turnover = turnover == null?0.0:turnover;

        //2.获取有效订单数
        Integer validOrderCount = ordersMapper.countByMap(map);
        //3.获取订单总数
        map.put("status", null);
        Integer totalOrderCount = ordersMapper.countByMap(map);

        //4.求订单完成率
        Double orderCompletionRate = 0.0;
        if (totalOrderCount!=0){
            orderCompletionRate = validOrderCount.doubleValue()/totalOrderCount;
        }

        //5.平均客单价 = 营业额/有效订单量
        Double unitPrice = turnover/validOrderCount;

        //6.新增用户数
        Integer newUserCount = userMapper.countByMap(map);

        //7.把前面的数据封装成BusinessDataVO对象，并返回
        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .newUsers(newUserCount)
                .validOrderCount(validOrderCount)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .orderCompletionRate(orderCompletionRate)
                .build();
        return businessDataVO;
    }

    @Override
    public OrderOverViewVO getOrderOverView() {
        //获得当天的开始时间
        LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
        //获得当天的结束时间
        LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX);
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        //1.获取待派送的订单数
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer toBeConfimredCount = ordersMapper.countByMap(map);

        //2.获取待派送的订单数
        map.put("status",Orders.CONFIRMED);
        Integer confirmedCount = ordersMapper.countByMap(map);

        //3.获取已完成的订单数
        map.put("status",Orders.COMPLETED);
        Integer completedCount = ordersMapper.countByMap(map);

        //4.获取已经取消的订单数
        map.put("status",Orders.CANCELLED);
        Integer cancelCount = ordersMapper.countByMap(map);

        //5.获取全部订单的数量
        map.put("status",null);
        Integer allCount = ordersMapper.countByMap(map);

        //6.把上面获取的到数据封装为OrderOverViewVO对象返回
        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder()
                .waitingOrders(toBeConfimredCount)
                .allOrders(allCount)
                .completedOrders(completedCount)
                .deliveredOrders(confirmedCount)
                .cancelledOrders(cancelCount)
                .build();

        return orderOverViewVO;
    }

    @Override
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap();
        //1.查询菜品起售数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);
        //2.查询菜品停售的数量
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 获取套餐总览的数据
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap();

        //1.查询套餐起售数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);

        //2.查询套餐停售的数量
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
